<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="应用名称" prop="appName">
      <el-input v-model="dataForm.appName" placeholder="应用名称"></el-input>
    </el-form-item>
    <el-form-item label="应用描述" prop="appDesc">
      <el-input v-model="dataForm.appDesc" placeholder="应用描述"></el-input>
    </el-form-item>
    <el-form-item label="应用唯一码" prop="uniqueId">
      <el-input v-model="dataForm.uniqueId" placeholder="应用唯一码"></el-input>
    </el-form-item>
    <el-form-item label="应用标记" prop="uniqueAppId">
      <el-input v-model="dataForm.uniqueAppId" placeholder="应用标记"></el-input>
    </el-form-item>
    <el-form-item label="应用密码要求等级1，2，3，4" prop="appLevel">
      <el-input v-model="dataForm.appLevel" placeholder="应用密码要求等级1，2，3，4"></el-input>
    </el-form-item>
    <el-form-item label="使用算法集合 ,分割" prop="arithList">
      <el-input v-model="dataForm.arithList" placeholder="使用算法集合 ,分割"></el-input>
    </el-form-item>
    <el-form-item label="状态  0：禁用   1：正常" prop="appStatus">
      <el-input v-model="dataForm.appStatus" placeholder="状态  0：禁用   1：正常"></el-input>
    </el-form-item>
    <el-form-item label="类型  1：政务外网  2：互联网" prop="appType">
      <el-input v-model="dataForm.appType" placeholder="类型  1：政务外网  2：互联网"></el-input>
    </el-form-item>
    <el-form-item label="归属机构" prop="deptId">
      <el-input v-model="dataForm.deptId" placeholder="归属机构"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="归属机构上级" prop="parentDeptId">
      <el-input v-model="dataForm.parentDeptId" placeholder="归属机构上级"></el-input>
    </el-form-item>
    <el-form-item label="是否删除  -1：已删除  0：正常" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="是否删除  -1：已删除  0：正常"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          appId: 0,
          appName: '',
          appDesc: '',
          uniqueId: '',
          uniqueAppId: '',
          appLevel: '',
          arithList: '',
          appStatus: '',
          appType: '',
          deptId: '',
          createTime: '',
          parentDeptId: '',
          delFlag: ''
        },
        dataRule: {
          appName: [
            { required: true, message: '应用名称不能为空', trigger: 'blur' }
          ],
          appDesc: [
            { required: true, message: '应用描述不能为空', trigger: 'blur' }
          ],
          uniqueId: [
            { required: true, message: '应用唯一码不能为空', trigger: 'blur' }
          ],
          uniqueAppId: [
            { required: true, message: '应用标记不能为空', trigger: 'blur' }
          ],
          appLevel: [
            { required: true, message: '应用密码要求等级1，2，3，4不能为空', trigger: 'blur' }
          ],
          arithList: [
            { required: true, message: '使用算法集合 ,分割不能为空', trigger: 'blur' }
          ],
          appStatus: [
            { required: true, message: '状态  0：禁用   1：正常不能为空', trigger: 'blur' }
          ],
          appType: [
            { required: true, message: '类型  1：政务外网  2：互联网不能为空', trigger: 'blur' }
          ],
          deptId: [
            { required: true, message: '归属机构不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          parentDeptId: [
            { required: true, message: '归属机构上级不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '是否删除  -1：已删除  0：正常不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.appId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.appId) {
            this.$http({
              url: this.$http.adornUrl(`/sys/subapplication/info/${this.dataForm.appId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.appName = data.subApplication.appName
                this.dataForm.appDesc = data.subApplication.appDesc
                this.dataForm.uniqueId = data.subApplication.uniqueId
                this.dataForm.uniqueAppId = data.subApplication.uniqueAppId
                this.dataForm.appLevel = data.subApplication.appLevel
                this.dataForm.arithList = data.subApplication.arithList
                this.dataForm.appStatus = data.subApplication.appStatus
                this.dataForm.appType = data.subApplication.appType
                this.dataForm.deptId = data.subApplication.deptId
                this.dataForm.createTime = data.subApplication.createTime
                this.dataForm.parentDeptId = data.subApplication.parentDeptId
                this.dataForm.delFlag = data.subApplication.delFlag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/subapplication/${!this.dataForm.appId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'appId': this.dataForm.appId || undefined,
                'appName': this.dataForm.appName,
                'appDesc': this.dataForm.appDesc,
                'uniqueId': this.dataForm.uniqueId,
                'uniqueAppId': this.dataForm.uniqueAppId,
                'appLevel': this.dataForm.appLevel,
                'arithList': this.dataForm.arithList,
                'appStatus': this.dataForm.appStatus,
                'appType': this.dataForm.appType,
                'deptId': this.dataForm.deptId,
                'createTime': this.dataForm.createTime,
                'parentDeptId': this.dataForm.parentDeptId,
                'delFlag': this.dataForm.delFlag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
